package com.yi.mall.goods.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yi.mall.common.model.DTO.ProductDTO;
import com.yi.mall.common.model.DTO.StockDTO;
import com.yi.mall.common.model.SkuInfo;
import com.yi.mall.goods.mapper.SkuInfoMapper;
import com.yi.mall.goods.service.ICategoryService;
import com.yi.mall.goods.service.ISkuInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author zzy
 * @since 2025-07-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo> implements ISkuInfoService {
    @Autowired
    private ICategoryService categoryService;
    @Transactional(readOnly = true)
    @Override
    public Page page(ProductDTO product) {
        // 1 构建条件构造器
        LambdaQueryChainWrapper<SkuInfo> wrapper = new LambdaQueryChainWrapper<>(baseMapper);
        // 2 填充条件
        // 2.1 按三级类别id查询
        if (product.getCategoryId() != null) {
            wrapper.eq(SkuInfo::getCategoryId, product.getCategoryId());
        }
        // 2.2 按二级类别id查询
        if (product.getTwoCategoryId() != null) {
            List<Integer> threeIds = categoryService.listAllByParentId(product.getTwoCategoryId());
            wrapper.in(SkuInfo::getCategoryId, threeIds);
        }
        // 2.3 按一级类别id查询
        if (product.getOneCategoryId() != null) {
            List<Integer> twoIds = categoryService.listAllByParentId(product.getOneCategoryId());
            List<Integer> threeIds = categoryService.listAllByParentId(twoIds);
            wrapper.in(SkuInfo::getCategoryId, threeIds);
        }
        // 2.4 按品牌id查询
        if(product.getBrandId() != null){
            wrapper.eq(SkuInfo::getBrandId, product.getBrandId());
        }
        // 2.5 按名称查询
        if(product.getSkuName() != null){
            wrapper.like(SkuInfo::getSkuName, product.getSkuName());
        }
        // 2.6 筛选列
        wrapper.select(SkuInfo::getId, SkuInfo::getSkuName, SkuInfo::getPrice, SkuInfo::getSkuDefaultImg);
        // 3 分页
        // 3.1 创建分页对象
        Page page = new Page();
        // 3.2 设置当前页码
        if(product.getCurrent() != null){
            page.setCurrent(product.getCurrent());
        }
        // 3.3 设置每页大小
        if(product.getSize() != null){
            page.setSize(product.getSize());
        }
        return wrapper.page(page);
    }
    @Transactional(readOnly = true)
    @Override
    public void dcount(List<StockDTO> stocks) throws Exception {
        /*
         * 遍历购物车数据
         * 如果找到库存商品，计算库存剩余量
         * 如果库存剩余量>=0，修改库存，否则抛出异常
         */
//        for (StockDTO stock : stocks) {
//            SkuInfo skuInfo = baseMapper.selectById(stock.getSkuId());
//            if(skuInfo != null){
//                int margin =  skuInfo.getNum() - stock.getNum();
//                if(margin >= 0){
//                    // 条件构造器
//                    LambdaUpdateChainWrapper<SkuInfo> updateChainWrapper = new LambdaUpdateChainWrapper<>(baseMapper);
//                    // 只修改num字段的值
//                    updateChainWrapper.eq(SkuInfo::getId,skuInfo.getId())
//                            .set(SkuInfo::getNum,margin).update();
//                }else {
//                    throw  new Exception("库存不足");
//                }
//            }
//        }
        List<String> ids = stocks.stream().map(stock -> {
            String skuId = stock.getSkuId();
            return skuId;
        }).collect(Collectors.toList());
        List<SkuInfo> skuInfos = baseMapper.selectBatchIds(ids);
        for (SkuInfo skuInfo : skuInfos) {
            for (StockDTO stock : stocks) {
                if(skuInfo.getId().equals(stock.getSkuId())){
                    int margin =  skuInfo.getNum() - stock.getNum();
                    if(margin >= 0){
                        // 条件构造器
                        LambdaUpdateChainWrapper<SkuInfo> updateChainWrapper = new LambdaUpdateChainWrapper<>(baseMapper);
                        // 只修改num字段的值
                        updateChainWrapper.eq(SkuInfo::getId,skuInfo.getId())
                                .set(SkuInfo::getNum,margin).update();
                    }
                    else {
                        throw  new Exception("库存不足");
                    }
                }
            }
        }

    }
}
